package top.jfunc.common.datetime;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * 时间起止
 */
public class TimeRangeBean {
	private final LocalDateTime from;
	private final LocalDateTime to;

	public TimeRangeBean(LocalDateTime from, LocalDateTime to) {
		this.to = to;
		this.from = from;
	}
	public static TimeRangeBean of(LocalDateTime from, LocalDateTime to) {
		return new TimeRangeBean(from, to);
	}

	/**
	 * 从此刻算起过去一个月【零点】
	 * 例如： 2024-03-22 00:00:00---2024-04-22 00:00:00
	 */
	public static TimeRangeBean passMonth() {
		LocalDateTime to = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
		LocalDateTime from = to.minusMonths(1);
		return new TimeRangeBean(from, to);
	}

	/**
	 * 上一个月【零点】
	 * 例如：2024-03-01 00:00:00---2024-03-31 00:00:00
	 */
	public static TimeRangeBean lastMonth() {
		LocalDate lastMonth = LocalDate.now().minusMonths(1);
		LocalDate from = lastMonth.with(TemporalAdjusters.firstDayOfMonth());
		LocalDate to = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
		return new TimeRangeBean(LocalDateTime.of(from, LocalTime.MIN), LocalDateTime.of(to, LocalTime.MIN));
	}

	/**
	 * 这一个月的第一天到最后一天
	 * 例如： 2024-04-01 00:00:00---2024-04-30 00:00:00
	 */
	public static TimeRangeBean thisMonth() {
		LocalDate lastMonth = LocalDate.now();
		LocalDate from = lastMonth.with(TemporalAdjusters.firstDayOfMonth());
		LocalDate to = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
		return new TimeRangeBean(LocalDateTime.of(from, LocalTime.MIN), LocalDateTime.of(to, LocalTime.MIN));
	}
	/**
	 * 这一个月的第一天到今天的零点
	 * 例如： 2024-04-01 00:00:00---2024-04-22 00:00:00
	 */
	public static TimeRangeBean thisMonthNow() {
		LocalDate now = LocalDate.now();
		LocalDate from = now.with(TemporalAdjusters.firstDayOfMonth());
		return new TimeRangeBean(LocalDateTime.of(from, LocalTime.MIN), LocalDateTime.of(now, LocalTime.MIN));
	}

	/**
	 * 昨日0点到最后一秒
	 * 2024-04-21 00:00:00---2024-04-21 23:59:59
	 */
	public static TimeRangeBean lastDay() {
		LocalDate now = LocalDate.now().minusDays(1);
		return new TimeRangeBean(LocalDateTime.of(now, LocalTime.MIN), LocalDateTime.of(now, LocalTime.MAX));
	}
	/**
	 * 今日日0点到最后一秒
	 * 2024-04-22 00:00:00---2024-04-22 23:59:59
	 */
	public static TimeRangeBean thisDay() {
		LocalDate now = LocalDate.now();
		return new TimeRangeBean(LocalDateTime.of(now, LocalTime.MIN), LocalDateTime.of(now, LocalTime.MAX));
	}

	/**
	 * 当前时间的整点-1 ~ 当前时间的整点，比如2024年1月1日13:10:00计算就是2024年1月1日12:00:00 ~ 2024年1月1日13:00:00
	 * 例如： 2024-04-22 16:00:00---2024-04-22 17:00:00
	 */
	public static TimeRangeBean integerPassHour() {
		LocalDateTime to = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
		LocalDateTime from = to.minusHours(1);
		return new TimeRangeBean(from, to);
	}

	/**
	 * 过去一分钟
	 * 例如： 2024-04-22 17:28:00---2024-04-22 17:29:00
	 */
	public static TimeRangeBean integerPassMinute() {
		LocalDateTime to = LocalDateTime.now().withSecond(0).withNano(0);
		LocalDateTime from = to.minusMinutes(1);
		return new TimeRangeBean(from, to);
	}

	/**
	 * 给定一个时间，计算其整点20分钟
	 * 例如： 2024-04-22 17:00:00---2024-04-22 17:20:00
	 */
	public static TimeRangeBean integerPass20Minute(LocalDateTime dateTime) {
		return integerPassMinute(dateTime, 20);
	}
	public static TimeRangeBean integerPassMinute(LocalDateTime dateTime, int minute) {
		dateTime = dateTime.withSecond(0).withNano(0);

		/*if(dateTime.getMinute()<20){
			dateTime = dateTime.withMinute(0);
		}else if(dateTime.getMinute()<40){
			dateTime = dateTime.withMinute(20);
		}else {
			dateTime = dateTime.withMinute(40);
		}*/
		dateTime = dateTime.withMinute(dateTime.getMinute() / minute * minute);
		LocalDateTime from = dateTime.minusMinutes(minute);
		return new TimeRangeBean(from, dateTime);
	}

	/**
	 * 把一个日期类型的扩展为当月的范围
	 * 2025-09-18转换为下面的
	 * 2025-09-01 00:00:00 --> 2025-09-30 23:59:59
	 */
	public static TimeRangeBeanAdjuster inflateMonth(DateTimeFormatter formatter){
		return src -> {
			LocalDate date = LocalDate.parse(src,formatter);
			return TimeRangeBean.of(date.with(TemporalAdjusters.firstDayOfMonth()).atTime(LocalTime.MIN),
					date.with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX));
		};
	}
	/**
	 * 把一个日期类型的扩展为当星期的范围
	 * 2025-09-18转换为下面的
	 * 2025-09-15 00:00:00 --> 2025-09-20 23:59:59
	 */
	public static TimeRangeBeanAdjuster inflateWeek(DateTimeFormatter formatter){
		return src -> {
			LocalDate date = LocalDate.parse(src,formatter);
			return TimeRangeBean.of(date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atTime(LocalTime.MIN),
					date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)).atTime(LocalTime.MAX));
		};
	}
	/**
	 * 把一个日期类型的扩展为当天的范围
	 * 2025-09-18转换为下面的
	 * 2025-09-18 00:00:00 --> 2025-09-18 23:59:59
	 */
	public static TimeRangeBeanAdjuster inflateDayDate(DateTimeFormatter formatter){
		return src -> {
			LocalDate date = LocalDate.parse(src,formatter);
			return TimeRangeBean.of(date.atTime(LocalTime.MIN), date.atTime(LocalTime.MAX));
		};
	}
	/**
	 * 把一个时间类型的扩展为当天的范围
	 * 2025-09-18 12:01:45转换为下面的
	 * 2025-09-18 00:00:00 --> 2025-09-18 23:59:59
	 */
	public static TimeRangeBeanAdjuster inflateDayByDatetime(DateTimeFormatter formatter){
		return src -> {
			LocalDateTime dateTime = LocalDateTime.parse(src,formatter);
			return TimeRangeBean.of(dateTime.with(LocalTime.MIN), dateTime.with(LocalTime.MAX));
		};
	}
	/**
	 * 把一个时间类型的扩展为当前小时的范围
	 * 2025-09-18 12:01:45转换为下面的
	 * 2025-09-18 12:00:00 --> 2025-09-18 12:59:59
	 */
	public static TimeRangeBeanAdjuster inflateHour(DateTimeFormatter formatter){
		return src -> {
			LocalDateTime dateTime = LocalDateTime.parse(src,formatter);
			dateTime = dateTime.withNano(0);
			return TimeRangeBean.of(dateTime.withMinute(0).withSecond(0), dateTime.withMinute(59).withSecond(59));
		};
	}
	/**
	 * 把一个时间类型的扩展为当前分钟的范围
	 * 2025-09-18 12:01:45转换为下面的
	 * 2025-09-18 12:01:00 --> 2025-09-18 12:01:59
	 */
	public static TimeRangeBeanAdjuster inflateMinute(DateTimeFormatter formatter){
		return src -> {
			LocalDateTime dateTime = LocalDateTime.parse(src,formatter);
			dateTime = dateTime.withNano(0);
			return TimeRangeBean.of(dateTime.withSecond(0), dateTime.withSecond(59));
		};
	}







	public LocalDateTime getFrom() {
		return from;
	}

	public String getFromAsString(String pattern) {
		return from.format(DateTimeFormatter.ofPattern(pattern));
	}
	public String getFromAsString(DateTimeFormatter formatter) {
		return from.format(formatter);
	}
	public LocalDateTime getTo() {
		return to;
	}

	public String getToAsString(String pattern) {
		return to.format(DateTimeFormatter.ofPattern(pattern));
	}
	public String getToAsString(DateTimeFormatter formatter) {
		return to.format(formatter);
	}

}
